<?php
ls_import('lslib/Error');
/**
* SqlQueryBuilder Class
* Generates simple SQL queries dynamically
* Please report any bug
* See documentation
*
* @author David Regla Demaree <dreglad@gmail.com>
* 
* Modified By LemanStore for LSLib
* @author Leman Kwok <me@lemanstore.com>
*/ 

class SqlQueryBuilder{
	var $VERSION = "1.3"; 
	var $context;
	var $type;
	var $table;
	var $columns;
	var $values;
	var $query;
	var $groupBy;
	var $having;
	var $orderBy;
	var $limit;
	
    /**
     * Constructor - Defines the query type
     *
     * @param string $type
     */ 
	function SqlQueryBuilder(&$context,$type='select'){
		$this->context = &$context;
		$this->type = strtoupper($type); 
		
	}
	
    /**
     * isValidType - Check the query type
     *
     * @param string $type
     * @return boolean 
     */ 
	function isValidType($type=''){
		return (in_array(array('INSERT','SELECT','UPDATE','REMOVE'),$type));
	}
	
	function setTable($table='',$alias=''){
		$this->table = "$table $alias";	
	}
	
	function addColumn($colName) {
		if(!in_array($this->columns,$colName))
		$this->columns[] = $colName;
    } 
    
	function addValue($colName,$value) {
		if(!in_array($this->columns,$colName)) $this->addColumn($colName);
		$value = (get_magic_quotes_gpc()) ? $value : addslashes($value);
		$this->values[$colName] = "'".$value."'";
    }
    
    function setWhere($where) {
        $this->where = $where;    
    }
    
    
    function setGroupBy($groupBy) {
        $this->groupBy = $groupBy;
    }
    
    
    function setHaving($having) {
        $this->having = $having;
    }
    
    
    function setOrderBy($orderBy) {
        $this->orderBy = $orderBy;
    }
    
    function setQuery($query) {
        $this->query = $query;
    }
    
    function setLimit($limit) {
        $this->limit = $limit;
    } 
    
    function buildQuery() {
        if (empty($this->table) and ($this->type != "QUERY")) return new Error("No table selected");
        
        $sqlString = "";
        switch ($this->type) {
            case "SELECT":
                $sqlString.= "SELECT ";
                $sqlString.= implode(", ", $this->columns);
                $sqlString.=" FROM ".$this->context->config['DatabaseTablePrefix'].$this->table."";
                
                if ($this->where)   $sqlString.= " WHERE $this->where";
                if ($this->groupBy) $sqlString.= " GROUP BY $this->groupBy";
                if ($this->having)  $sqlString.= " HAVING $this->having";
                if ($this->orderBy) $sqlString.= " ORDER BY $this->orderBy";
                if ($this->limit)   $sqlString.= " LIMIT $this->limit";
                
                break;
            case "INSERT":
                if (count($this->columns) != count($this->values)) return $this->error("Error - Column list doesn't match the value list");
                $sqlString.= "INSERT INTO {$this->table} ";
                
                $sqlString.= "(";
                $sqlString.= implode(", ", $this->columns);
                $sqlString.= ") ";
                
                $sqlString.= "VALUES";
                
                $sqlString.= " (";
                $sqlString.= implode(", ", $this->values);
                $sqlString.= ")";
                break;
            case "UPDATE":
                if (count($this->columns) != count($this->values)) return $this->error("Error - Column list doesn't match the value list");
                $sqlString.= "UPDATE ".$this->context->config['DatabaseTablePrefix'].$this->table." SET ";
                
                $noColumns = count($this->columns);
                for ($i=0;$i<$noColumns;$i++) {
                    $sqlString.= "{$this->columns[$i]} = {$this->values[$i]}";
                    if ($i < $noColumns-1) $sqlString.= ", ";
                }
                    
                
                if ($this->where) $sqlString.= " WHERE $this->where";
                if ($this->limit) $sqlString.= " LIMIT $this->limit";
                
                break;
            case "DELETE":
                $sqlString.= "DELETE FROM ".$this->context->config['DatabaseTablePrefix'].$this->table." ";
                
                if ($this->where) $sqlString.= "WHERE $this->where";
                break;
            case "QUERY":
                if (!$this->query) return new Error("There's no SQL");
                $sqlString.= $this->query;
                
        }
        
        $sqlString.=";";
        return $sqlString;
    } 
    
}
?>